/**往期功能 */
const router = require('koa-router')();
const DB = require('../db');

/**
 * 添加往期功能
 * @param {string} content 内容
 * @arg {array} contriId 贡献id名单 
 */
router.post('/addPreviousFonc', async (ctx, next) => {

  const userName = ctx.request.header.username;

  const { content, contriId } = ctx.request.body;

  if (!userName || !content || !contriId) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }

    return;
  }

  if (!Array.isArray(contriId)) {
    ctx.body = {
      code: '0001',
      message: '传入参数类型错误',
      data: {}
    }

    return;
  }

  if (contriId.length === 0) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }

    return;
  }

  let avatarArr = [];
  for (let i = 0; i < contriId.length; i++) {
    const ra = await DB.find('users', { uid: contriId[i] });
    avatarArr.push(ra[0].avatarUrl);
  }

  const data = {
    key: String(Date.now()) + '_' + userName,
    publishDate: Date.now(),
    content,
    contriId,
    avatarArr,
    likeNum: 0,
    likeList: []
  };

  await DB.add('previousFonc', data);

  ctx.body = {
    code: '0000',
    message: '添加成功',
    data
  }
})

/**
 * 获取往期功能列表
 */
router.get('/previousList', async (ctx, next) => {
  const r = await DB.find('previousFonc');
  const l = r.sort((a, b) => b.key.slice(0, b.key.indexOf('_')) - a.key.slice(0, a.key.indexOf('_')));
  ctx.body = {
    code: '0000',
    message: '获取成功',
    data: l
  }
})

/**
 * 删除往期功能
 * @param {string} id 指定id，对应key值
 * @requires userName 请求头必须有userName
 */
router.post('/deletePreviosItem', async (ctx, next) => {
  const userName = ctx.request.header.username;
  const { id } = ctx.request.body;

  if (!userName || !id) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }

  const r = await DB.find('previousFonc', { key: id });

  if (!r || r.length === 0) {
    ctx.body = {
      code: '0013',
      messge: '未找到要删除的内容',
      data: {}
    }
    return
  }

  await DB.delOne('previousFonc', { key: id });

  ctx.body = {
    code: '0000',
    message: '删除成功',
    data: r[0]
  }
})

/**
 * 编辑往期功能
 * @param {string} id 对应的key
 * @requires userName 
 */
router.post('/editPrevios', async (ctx, next) => {
  const userName = ctx.request.header.username;
  const { id, content, contriId } = ctx.request.body;

  if (!userName || !id || !content || !contriId) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }

  if (!Array.isArray(contriId)) {
    ctx.body = {
      code: '0001',
      message: '传入参数类型错误',
      data: {}
    }
    return;
  }

  if (contriId.length === 0) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }

  let avatarArr = [];
  for (let i = 0; i < contriId.length; i++) {
    const ra = await DB.find('users', { uid: contriId[i] });
    avatarArr.push(ra[0].avatarUrl);
  }

  await DB.updateValue('previousFonc', { key: id }, { content, contriId, avatarArr });
  const r = await DB.find('previousFonc', { key: id });
  ctx.body = {
    code: '0000',
    message: '修改成功',
    data: r[0]
  }
})

/**
 * 往期功能点赞
 * @param {string} id 对应的key值
 */
router.post('/addPreviosLike', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0010',
      message: '登录已过期',
      data: {}
    }
    return;
  }
  const { id } = ctx.request.body;
  if (!id) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }
  const r0 = await DB.find('previousFonc', { key: id });
  if (!r0 || r0.length === 0) {
    ctx.body = {
      code: '0013',
      message: '传入id错误，点赞失败',
      data: {}
    }
    return;
  }
  const newLike = r0[0].likeNum + 1;
  const newList = r0[0].likeList;
  newList.push(userName);
  await DB.updateValue('previousFonc', { key: id }, { likeNum: newLike, likeList: newList });
  ctx.body = {
    code: '0000',
    data: '点赞成功',
    data: Object.assign({}, r0[0], { likeNum: newLike, likeList: newList })
  }
})

/**
 * 往期功能取消点赞
 * @param {string} id 对应key值
 */
router.post('/cutPreviousLike', async (ctx, next) => {
  const userName = ctx.request.header.username;
  if (!userName) {
    ctx.body = {
      code: '0010',
      message: '登录已过期',
      data: {}
    }
    return;
  }
  const { id } = ctx.request.body;
  if (!id) {
    ctx.body = {
      code: '0002',
      message: '缺少必要参数',
      data: {}
    }
    return;
  }
  const r0 = await DB.find('previousFonc', { key: id });
  if (!r0 || r0.length === 0) {
    ctx.body = {
      code: '0013',
      message: '传入id错误，取消点赞失败',
      data: {}
    }
    return;
  }
  const newLike = r0[0].likeNum - 1;
  const oldList = r0[0].likeList;
  oldList.splice(oldList.indexOf(userName), 1);
  await DB.updateValue('previousFonc', { key: id }, { likeNum: newLike, likeList: oldList });
  ctx.body = {
    code: '0000',
    data: '点赞成功',
    data: Object.assign({}, r0[0], { likeNum: newLike, likeList: oldList })
  }
})

module.exports = router;